Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runner::processFile()/error handling: add more defensive coding #625

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Oct 3, 2024

Description

Follow up to squizlabs/PHP_CodeSniffer#3716 and PR #524.

PR squizlabs/PHP_CodeSniffer#3716 improved the information provided in the error messages for Internal.Exceptions with additional information about the source of the problem. This code uses the Common::getSniffCode() method to transform a sniff class name into a sniff code.

PR #524 changes how the Common::getSniffCode() method handles invalid input, i.e. it will now throw an exception when a class is passed which isn't a sniff (or sniff test).

In the context of this error handling, however, this new exception can easily be encountered when an Abstract...Sniff class contains the code which triggered the error - which would trigger the exception as it is not a sniff class.

This commit now adds some additional defensive coding to ensure that a) the error message will still be informative, while b) making sure that any exception which may be thrown by the Common::getSniffCode() method will be caught and and won't cause an uncaught InvalidArgumentException in the error handling code to stop execution of the script.

Includes a test documenting the behaviour of the Common::getSniffCode() method for abstract sniff classes.

Suggested changelog entry

should be covered via the changelog entry for #524

Follow up to squizlabs/PHP_CodeSniffer 3716 and PR 524.

PR squizlabs/PHP_CodeSniffer 3716 improved the information provided in the error messages for `Internal.Exception`s with additional information about the source of the problem.
This code uses the `Common::getSniffCode()` method to transform a sniff class name into a sniff code.

PR 524 changes how the `Common::getSniffCode()` method handles invalid input, i.e. it will now throw an exception when a class is passed which isn't a sniff (or sniff test).

In the context of this error handling, however, this new exception can easily be encountered when an `Abstract...Sniff` class contains the code which triggered the error - which would trigger the exception as it is not a sniff class.

This commit now adds some additional defensive coding to ensure that a) the error message will still be informative, while b) making sure that any exception which may be thrown by the  `Common::getSniffCode()` method will be caught and and won't cause an uncaught `InvalidArgumentException` in the error handling code to stop execution of the script.

Includes a test documenting the behaviour of the `Common::getSniffCode()` method for abstract sniff classes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant